package org.dromara.workflow.rocketmq.leave;

import com.cloud.rocketmq.base.entity.CloudMessage;
import com.cloud.rocketmq.spring.annotation.ConsumeTopic;
import com.cloud.rocketmq.spring.core.TopicListener;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.json.utils.JsonUtils;
import org.dromara.common.tenant.helper.TenantHelper;
import org.dromara.workflow.api.event.ProcessTaskEvent;
import org.dromara.workflow.mapper.TestLeaveMapper;

import static org.dromara.common.core.constant.RocketMQConstants.FlowTopic.*;

/**
 * 执行任务创建监听
 * 示例：也可通过  @EventListener(condition = "#processCreateTaskEvent.flowCode=='leave1'")进行判断
 * 在方法中判断流程节点key
 * if ("xxx".equals(processCreateTaskEvent.getNodeCode())) {
 * //执行业务逻辑
 * }
 */
@Slf4j
@ConsumeTopic(topic = FLOW_PROCESS_TOPIC_NAME, eventCode = FLOW_PROCESS_CREATE_TASK_TAG_NAME, log = true)
@RequiredArgsConstructor
public class ProcessCreateTaskListener implements TopicListener<ProcessTaskEvent> {

    private final TestLeaveMapper testLeaveMapper;

    @Override
    public void onMessage(CloudMessage<ProcessTaskEvent> processTaskMessage) {
        log.info("执行任务创建监听消息:{}", JsonUtils.toJsonString(processTaskMessage));
        ProcessTaskEvent processTaskEvent = processTaskMessage.getPayload();
        if (processTaskEvent.getFlowCode().startsWith("leave")) {
            TenantHelper.dynamic(processTaskEvent.getTenantId(), () -> {
                log.info("当前任务创建了{}", processTaskEvent);
                /*if (BusinessStatusEnum.WAITING.getStatus().equals(processCreateTaskEvent.getStatus())) {
                    TestLeave testLeave = testLeaveMapper.selectById(Long.valueOf(processCreateTaskEvent.getBusinessId()));
                    testLeave.setStatus(BusinessStatusEnum.WAITING.getStatus());
                    testLeaveMapper.updateById(testLeave);
                }*/
            });
        }
    }

}
